package com.smart.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.mobile.dao.MarketGoodsDAO;
import com.smart.mobile.dao.MarketInfoDAO;
import com.smart.mobile.dao.AddressAddDAO;
import com.smart.mobile.dao.AddressDelDao;
import com.smart.mobile.dao.AddressEditDAO;
import com.smart.mobile.dao.AddressInfoDAO;
import com.smart.model.mobile.MarketGoods;
import com.smart.model.mobile.MarketInfo;
import com.smart.model.mobile.AddressInfo;
import com.smart.util.ImgEncode;
import com.smart.util.JsonHelper;

import net.sf.json.JSONArray;

@Controller
public class MobileController {
	String img_market = "\\media\\market\\";//商家图片 
	String img_goods = "\\media\\market\\";//商品图片
	String img_mini = "\\media\\market\\";//商家小图
	//显示商场信息
	@RequestMapping(value="/mobile/test", method = RequestMethod.POST)
	@ResponseBody
	public List<MarketInfo> getMarketInfo(HttpServletRequest req,HttpServletResponse resp) throws SQLException{
		MarketInfoDAO dao = new MarketInfoDAO();
		List<MarketInfo> list = dao.getMarketInfo();
		try {
			String ImgURL = req.getServletContext().getRealPath("")+this.img_market;
			System.out.println(req.getServletContext().getRealPath(""));
			System.out.println("market number = "+list.size());
			//加载图片
			MarketInfo tempinfo; 
			for(int i=0;i<list.size();i++)
			{
				tempinfo = (MarketInfo) list.get(i);
				String imgid = tempinfo.getmarketimg();//获取图片ID
				tempinfo.setmarketimg_str(ImgEncode.getImgStr(ImgURL+imgid));
				String miniid = tempinfo.getMarketmini();
				tempinfo.setMarketmini_str(ImgEncode.getImgStr(ImgURL+miniid));
//				System.out.println("marketID:"+tempinfo.getID());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(MarketInfo info:list)
			System.out.println("MarketID:"+info.getID());
		return list;
		
	}
	@RequestMapping(value="/mobile/goods" ,method=RequestMethod.POST)
	@ResponseBody
	public List<MarketGoods> getGoods(HttpServletRequest req,HttpServletResponse resp) throws SQLException, IOException{
		int marketid = Integer.parseInt(req.getParameter("marketID"));
		System.out.println("marketid:"+marketid);
		//商品图片+商家小图
		String ImgURL = req.getServletContext().getRealPath("")+this.img_goods;
		String end;
		MarketGoodsDAO dao = new MarketGoodsDAO();
		List<MarketGoods> list = dao.getGoodsByMarketID(marketid);
		MarketGoods temp_goods;
		System.out.println("商品个数:"+list.size());
		for(int i=0;i<list.size();i++){
			temp_goods = list.get(i);
			end = temp_goods.getShoppic();
			temp_goods.setShoppic_str(ImgEncode.getImgStr(ImgURL+end));
			System.out.print(temp_goods.getId()+"	");
		}
		return list;
	}
//	@RequestMapping(value="/mobile/address" , method=RequestMethod.POST)
	
	@RequestMapping(value="/mobile/{userid}/address" , method=RequestMethod.GET)
	@ResponseBody
	public List<AddressInfo> getAddress(@PathVariable("userid") int userid) throws SQLException, IOException{
		System.out.println("userID:"+userid);
		AddressInfoDAO dao = new AddressInfoDAO();
		java.util.List<AddressInfo> list = null;
		try {
			list = dao.getAddressInfo(userid);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return list;
			
		
	}
	/*
	 * 地址添加/修改
	 * type = 1 新添地址
	 * type = 2 地址修改
	 * */
	@RequestMapping(value="/mobile/address_add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Boolean> updateAddress(@RequestParam("type") int type,@RequestBody AddressInfo address){
		Map<String,Boolean> result = new HashMap<String,Boolean>();
		//新建地址
		if(type == 1){ 
			AddressAddDAO dao = new AddressAddDAO(address);
			if(!dao.AddressAdd()){
				System.out.println("数据插入异常");
				result.put("result", false);
			}
			result.put("result", true);
		}
		//编辑地址
		else if(type ==2){ 
			AddressEditDAO dao = new AddressEditDAO(address);
			if(!dao.AddressEdit()){
				System.out.println("数据更新异常");
				result.put("result", false);
			}
			result.put("result", true);
		}
		else{
			result.put("result", false);
			System.out.println("编辑类型出错:type = "+type);
		}
		return result;
			
	}
	/*
	 * 删除地址
	 * */
	@RequestMapping(value="/mobile/address_del",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Boolean> deleteAddress(@RequestParam("userId")int userId,@RequestParam("addressId") int addressId){
		Map<String,Boolean> result = new HashMap<String,Boolean>();
		AddressDelDao dao = new AddressDelDao(userId,addressId);
		if(dao.AddressDelete()){	//删除成功
			result.put("result", true);
			System.out.println("Delete successfully! userid:"+userId+" id:"+addressId);
		}
			
		else{
			result.put("result", false);
			System.out.println("Delete failed");
		}
		return result;
		
	}
}
